package com.granada.utils;

import com.github.pagehelper.Page;
import com.granada.vo.resp.PageVO;

import java.util.List;

/**
* @ClassName:       PageUtils
*                   分页工具类
* @Author:          小霍
* @CreateDate:      2019/9/19 13:26
* @UpdateUser:      小霍
* @UpdateDate:      2019/9/19 13:26
* @Version:         0.0.1
*/
public class PageUtils {
	private PageUtils() {}

    public static <T> PageVO<T> getPageVO(List<T> list) {
        PageVO<T> result = new PageVO<>();
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            result.setTotalRows(page.getTotal());
            result.setTotalPages(page.getPages());
            result.setPageNum(page.getPageNum());
            result.setPageSize(page.getPageSize());
            result.setCurPageSize(page.size());
            result.setList(page.getResult());
        }
        return result;
    }

    /**
     *
     * @param list-PageVo返回的实际内容
     * @param list2-查询之后的Page
     * @param <T>
     * @return
     */
    public static <T> PageVO<T> getPageVO(List<T> list, List list2){
        PageVO<T> result = new PageVO<>();
        System.out.println("111"+list2);
       // Page page = (Page) list2;

        Page<T> page = (Page<T>)list2;
        result.setTotalRows(page.getTotal()); //条数
        result.setTotalPages(page.getPages());
        result.setPageNum(page.getPageNum());
        result.setPageSize(page.getPageSize());
        result.setCurPageSize(page.size());
        result.setList(list);
        return result;
    }

    public static <T> PageVO<T> getNewPageVO(List<T> list,PageVO pageVO) {
        PageVO<T> result = new PageVO<>();
        Page<T> page = (Page<T>) new Page<T>();
        result.setTotalRows(pageVO.getTotalRows()); //条数
        result.setTotalPages(pageVO.getTotalPages()); //-- 总页数
        result.setPageNum(pageVO.getPageNum()); //--当前第几页
        result.setPageSize(pageVO.getPageSize()); //--每页记录数
        result.setCurPageSize(pageVO.getCurPageSize()); //--当前页记录数
        result.setList(list);
        return result;
    }


}
